package geom;

import geom.Line;
import geom.Vector;

import java.util.ArrayList;
import java.util.Iterator;

import processing.core.PApplet;

public class TangentLine {
	ArrayList<Vector> points;
	
	
	
	public TangentLine(){
		points = new ArrayList<Vector>();
	}
	
	public void add(Vector point){
		points.add(point);
	}
	
	public ArrayList<Line> getLines(){
		ArrayList<Line> lines = new ArrayList<Line>();
		if(points.size() < 2) return lines;
		for(int i = 0; i < points.size()-1; i++){
			lines.add(new Line(points.get(i), points.get(i+1)));
		}
		return lines;
	}

	
	public void draw(PApplet pa){
		Iterator<Vector> i = points.iterator();
		pa.beginShape();
		while(i.hasNext()){
			Vector next = i.next();
			pa.vertex(next.x, next.y);
		}
		pa.endShape();
	}
	
	public void drawHeadings(PApplet pa){
		pa.beginShape();
		for(int i = 0; i < points.size(); i++){
			Vector head = getHeadingVector(i);
			Vector p1 = points.get(i).sub(head.mult(0.5f));
			Vector p2 = p1.add(head);
			pa.line(p1.x, p1.y, p2.x, p2.y);
		}
		pa.endShape();
	}

	public Vector getHeadingVector(int i){
		if(i < 0 || i >= points.size()) return null;
		Vector v1, v2;
		if(i > 0) v1 = points.get(i-1);
		else v1 = points.get(i);
		if(i < points.size()-1) v2 = points.get(i+1);
		else v2 = points.get(i);
		return v2.sub(v1);
	}

}
